﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Janus.Cores
{
    /// <summary>
    /// XML序列化与反序列化工具类
    /// </summary>
    public class XmlUtil
    {
        #region 反序列化
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static T Deserialize<T>(string xmlPath)
              where T : class
        {
            try
            {
                using (FileStream sr = new FileStream(xmlPath, FileMode.Open))
                {
                    XmlSerializer xmldes = new XmlSerializer(typeof(T));
                    return xmldes.Deserialize(sr) as T;
                }

            }
            catch (Exception e)
            {
                Loger.WriteLog(e);
                return null;
            }
        }

        public static T DeserializeForOpLog<T>(string xmlPath)
             where T : class
        {
            try
            {
                using (StreamReader sr = new StreamReader(xmlPath))
                {                    
                    string xml = sr.ReadToEnd();
                    xml = xml.Replace("<?xml version=\"1.0\"?>", "")
                        .Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "")
                        .Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
                    xml=string.Format("<?xml version=\"1.0\"?><ArrayOfOperationLogDTO xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">{0}</ArrayOfOperationLogDTO>", xml);
                    StringReader strR = new StringReader(xml);
                    XmlSerializer xmldes = new XmlSerializer(typeof(T));
                    return xmldes.Deserialize(strR) as T;
                }

            }
            catch (Exception e)
            {
                return null;
            }
        }

        #endregion

        #region 序列化
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serializer<T>(T t)
            where T : class
        {
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(typeof(T));
            try
            {
                //序列化对象
                xml.Serialize(Stream, t);
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            Stream.Position = 0;
            StreamReader sr = new StreamReader(Stream);
            string str = sr.ReadToEnd();

            sr.Dispose();
            Stream.Dispose();

            return str;
        }

        private static object obj = new object();
        /// <summary>
        /// 序列化并保存为xml文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="k"></param>
        /// <param name="xmlFilePath"></param>
        public static void SerializerToFile<T>(T t, string xmlFilePath)
            where T : class
        {
            lock (obj)
            {
                FileStream stream = null;
                if (File.Exists(xmlFilePath))
                { stream = new FileStream(xmlFilePath, FileMode.Append); }
                else
                {
                    stream = new FileStream(xmlFilePath, FileMode.Create);
                }
                XmlSerializer xmlserilize = new XmlSerializer(typeof(T));
                xmlserilize.Serialize(stream, t);
                stream.Close();
            }
        }

        #endregion
    }
}
